package backend;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;
import domain.*;
import java_persistence.*;


public class DomainDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		//This sets up the database.
		String myBatis_config = "resources/mybatis-config.xml";
		Reader config_reader = Resources.getResourceAsReader(myBatis_config);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(config_reader);
		SqlSession session = sessionFactory.openSession();
		
		//Now, we proceed with tests.
		
		//First, create a user.

		User u = new User();
		u.setUsername("jsimoneau");
		u.setActive_account(true);
		u.setAddress("123 Fake St Atlanta, GA 30031");
		u.setEmail("dummy@yahoo.com");
		u.setName("Joseph Simoneau");
		u.setPassword("password");
		u.setPhone_number("4045553456");
		
		u.create( session.getMapper(UserMapper.class) );
		
	
		List<User> users = new ArrayList<User>();
		
		users = session.getMapper(UserMapper.class).get_users();
	    //users = session.selectList("select * from users;");
		int c = 0; 
		for( User i : users) {
			System.out.println("Username: " + i.getUsername() );
			c++;
			
		}
		System.out.println( "Users: " + ((Integer)c).toString() );
		
		session.getMapper(UserMapper.class).delete("jsimoneau");
		users = session.getMapper(UserMapper.class).get_users();
		c = 0; 
		for( User i : users) {
			System.out.println("Username: " + i.getUsername() );
			c++;
			
		}
		System.out.println( "Users: " + ((Integer)c).toString() );
		
		
		session.commit();
		session.close();
	}

}
